package com.erp.sell.handler;

import com.erp.common.utils.poi.ExcelHandlerAdapter;
import com.erp.sell.domain.ErpSalProductsVO;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.List;

/**
 * 商品列表Excel处理器
 * 将商品列表格式化为易读的字符串
 * 
 * @author lzx
 * @date 2025/9/3
 */
public class ProductListHandler implements ExcelHandlerAdapter {
    
    @Override
    public Object format(Object value, String[] args, Cell cell, Workbook wb) {
        if (value == null) {
            return "";
        }
        
        if (value instanceof List) {
            @SuppressWarnings("unchecked")
            List<ErpSalProductsVO> productList = (List<ErpSalProductsVO>) value;
            
            if (productList.isEmpty()) {
                return "";
            }
            
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < productList.size(); i++) {
                ErpSalProductsVO product = productList.get(i);
                if (i > 0) {
                    sb.append("; ");
                }
                sb.append(product.getProductName())
                  .append(" - ￥").append(product.getPrice())
                  .append(" * ").append(product.getCount()).append(product.getUnit());
            }
            return sb.toString();
        }
        
        return value.toString();
    }
}
